Skip to content

[TRTLLM-14551][perf] avoid GDN state reset host synchronization#16716

Open
liji-nv wants to merge 1 commit into
NVIDIA:mainfrom
liji-nv:liji/avoid-gdn-state-reset-sync
Open

[TRTLLM-14551][perf] avoid GDN state reset host synchronization#16716
liji-nv wants to merge 1 commit into
NVIDIA:mainfrom
liji-nv:liji/avoid-gdn-state-reset-sync

Conversation

@liji-nv

@liji-nv liji-nv commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator

GDN prefill reset currently selects cache slots with a CUDA boolean mask before assigning zeros. The boolean-indexed selection has a data-dependent output shape, which forces the host to obtain the selected element count and introduces a stream synchronization for every GDN layer.

Replace the two indexed assignments with a fixed-shape Triton launch. Each program reads the request's cache index and initialization flag on device, validates the cache slot, and clears both the SSM and convolution state rows in one kernel. Requests with initialized state or invalid sentinel indices remain untouched.

Add a CUDA unit test covering reset slots, preserved initialized slots, untouched unrelated slots, and the negative invalid-index sentinel.

Dev Engineer Review

  • Added a Triton kernel helper (_reset_gdn_states_kernel / _reset_gdn_states) in tensorrt_llm/_torch/modules/mamba/gdn_mixer.py to reset eligible GDN state rows (SSM and convolution) for a batch of requests by using state_indices and has_initial_states directly on device.
  • Kernel behavior:
    • Loads state_idx and computes needs_reset = ~has_initial_states[request].
    • Validates state_idx via 0 <= state_idx < NUM_CACHE_LINES (treats -1 as invalid).
    • Zeroes ssm_states and conv_states only when needs_reset & valid_state, with additional bounds offsets < SSM_STATE_SIZE / offsets < CONV_STATE_SIZE (row-size gating).
    • Leaves initialized states unchanged; invalid sentinel indices are not zeroed.
  • Updated forward_core to replace the prior masked/host-influenced reset logic for prefills with a single call:
    • _reset_gdn_states(ssm_states, conv_states, state_indices_p, has_initial_states_p) when num_prefills > 0.
  • Review focus: correctness of Triton grid/shape assumptions (grid = (num_requests, cdiv(max(state_sizes), 256))), index/sentinel handling, and ensuring the masked store prevents any out-of-bounds effects even when state_idx is invalid.

QA Engineer Review

  • Added CUDA-only unit test test_reset_gdn_states_preserves_initialized_and_invalid_slots in tests/unittest/_torch/modules/mamba/test_gdn_kernel_optimizations.py.
    • Constructs a state_pool backing views for ssm_states/conv_states.
    • Calls _reset_gdn_states with state_indices=[1, 3, -1] and has_initial_states=[False, True, False].
    • Asserts:
      • Slot 1 is zeroed.
      • Initialized slot 3 is preserved.
      • Invalid -1 does not modify any unrelated slot(s).
  • Test coverage in tests/integration/test_lists/ (test-db/qa) for this specific unit test could not be verified from search results.
  • Verdict: needs follow-up.

Description

Test Coverage

PR Checklist

Please review the following before submitting your PR:

  • PR description clearly explains what and why. If using CodeRabbit's summary, please make sure it makes sense.

  • PR Follows TRT-LLM CODING GUIDELINES to the best of your knowledge.

  • Test cases are provided for new code paths (see test instructions)

  • If PR introduces API changes, an appropriate PR label is added - either api-compatible or api-breaking. For api-breaking, include BREAKING in the PR title.

  • Any new dependencies have been scanned for license and vulnerabilities

  • CODEOWNERS updated if ownership changes

  • Documentation updated as needed

  • Update tava architecture diagram if there is a significant design change in PR.

  • The reviewers assigned automatically/manually are appropriate for the PR.

  • Please check this after reviewing the above items as appropriate for this PR.

GitHub Bot Help

To see a list of available CI bot commands, please comment /bot help.

@liji-nv
liji-nv requested a review from a team as a code owner July 22, 2026 08:26
@liji-nv
liji-nv requested a review from aswinvisva July 22, 2026 08:26
@liji-nv

liji-nv commented Jul 22, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60933 [ run ] triggered by Bot. Commit: f8026d7 Link to invocation

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Enterprise

Run ID: 8c9fd5eb-ab24-4802-a733-96099d2f7494

📥 Commits

Reviewing files that changed from the base of the PR and between 57f5e21 and 2bd3f1e.

📒 Files selected for processing (2)
  • tensorrt_llm/_torch/modules/mamba/gdn_mixer.py
  • tests/unittest/_torch/modules/mamba/test_gdn_kernel_optimizations.py
🚧 Files skipped from review as they are similar to previous changes (2)
  • tests/unittest/_torch/modules/mamba/test_gdn_kernel_optimizations.py
  • tensorrt_llm/_torch/modules/mamba/gdn_mixer.py

Walkthrough

The GDN mixer adds a Triton helper to reset selected SSM and convolution cache states, replaces inline tensor assignments in forward_core, and adds a CUDA test covering valid, invalid, initialized, and uninitialized slots.

Changes

GDN state reset

Layer / File(s) Summary
Triton state reset helper
tensorrt_llm/_torch/modules/mamba/gdn_mixer.py
Adds _reset_gdn_states_kernel and _reset_gdn_states with masks for state flags, valid indices, and state-size bounds.
Forward integration and validation
tensorrt_llm/_torch/modules/mamba/gdn_mixer.py, tests/unittest/_torch/modules/mamba/test_gdn_kernel_optimizations.py
Routes forward_core through the helper and verifies that only the targeted valid slot is zeroed while other slots retain their values.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • NVIDIA/TensorRT-LLM#16748: Updates related GDN cache-reset behavior using indexed fills instead of the Triton reset kernel.

Sequence Diagram(s)

sequenceDiagram
  participant Qwen3NextGatedDeltaNet
  participant _reset_gdn_states
  participant _reset_gdn_states_kernel
  participant StateBuffers
  Qwen3NextGatedDeltaNet->>_reset_gdn_states: pass state buffers, indices, and initial-state flags
  _reset_gdn_states->>_reset_gdn_states_kernel: launch masked reset
  _reset_gdn_states_kernel->>StateBuffers: zero selected SSM and convolution slots
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title matches the PR's main change and includes the ticket and type in the expected format.
Description check ✅ Passed The description covers the issue, solution, and checklist, and the test addition is described even though the Test Coverage section is blank.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Comment @coderabbitai help to get the list of available commands.

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #60933 [ run ] completed with state SUCCESS. Commit: f8026d7
/LLM/main/L0_MergeRequest_PR pipeline #49198 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@nv-guomingz nv-guomingz changed the title [None][perf] avoid GDN state reset host synchronization [TRTLLM-14551][perf] avoid GDN state reset host synchronization Jul 22, 2026
@liji-nv
liji-nv force-pushed the liji/avoid-gdn-state-reset-sync branch from f8026d7 to 57f5e21 Compare July 23, 2026 03:07
@liji-nv

liji-nv commented Jul 23, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61176 [ run ] triggered by Bot. Commit: 57f5e21 Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61176 [ run ] completed with state FAILURE. Commit: 57f5e21
/LLM/main/L0_MergeRequest_PR pipeline #49425 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

Comment thread tensorrt_llm/_torch/modules/mamba/gdn_mixer.py Outdated
GDN prefill reset currently selects cache slots with a CUDA boolean mask before assigning zeros. The boolean-indexed selection has a data-dependent output shape, which forces the host to obtain the selected element count and introduces a stream synchronization for every GDN layer.

Replace the two indexed assignments with a fixed-shape Triton launch. Each program reads the request's cache index and initialization flag on device, validates the cache slot, and clears both the SSM and convolution state rows in one kernel. Requests with initialized state or invalid sentinel indices remain untouched.

Compute the state row offsets explicitly in int64. Recurrent-state views stride across the interleaved SSM and convolution pool, so multiplying an int32 cache index by either stride is not guaranteed to fit in int32 for a large pool.

Add a CUDA unit test covering reset slots, preserved initialized slots, untouched unrelated slots, and the negative invalid-index sentinel.

Signed-off-by: Jin Li <59594262+liji-nv@users.noreply.github.com>
@liji-nv
liji-nv force-pushed the liji/avoid-gdn-state-reset-sync branch from 57f5e21 to 2bd3f1e Compare July 24, 2026 02:39
@liji-nv

liji-nv commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast --add-multi-gpu-test

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61475 [ run ] triggered by Bot. Commit: 2bd3f1e Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61475 [ run ] completed with state FAILURE. Commit: 2bd3f1e
/LLM/main/L0_MergeRequest_PR pipeline #49695 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

@liji-nv

liji-nv commented Jul 24, 2026

Copy link
Copy Markdown
Collaborator Author

/bot run --disable-fail-fast --add-multi-gpu-test

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61562 [ run ] triggered by Bot. Commit: 2bd3f1e Link to invocation

@tensorrt-cicd

Copy link
Copy Markdown
Collaborator

PR_Github #61562 [ run ] completed with state FAILURE. Commit: 2bd3f1e
/LLM/main/L0_MergeRequest_PR pipeline #49773 completed with status: 'FAILURE'

CI Report

⚠️ Action Required:

  • Please check the failed tests and fix your PR
  • If you cannot view the failures, ask the CI triggerer to share details
  • Once fixed, request an NVIDIA team member to trigger CI again

CI Agent Failure Analysis

Link to invocation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants